package autocode

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/autocode"
	autoCodeReq "github.com/flipped-aurora/gin-vue-admin/server/model/autocode/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
)

type ProgramJoinInfoService struct {
}

// CreateProgramJoinInfo 创建ProgramJoinInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (programJoinInfoService *ProgramJoinInfoService) CreateProgramJoinInfo(programJoinInfo autocode.ProgramJoinInfo) (err error) {
	err = global.GVA_DB.Create(&programJoinInfo).Error
	return err
}

// DeleteProgramJoinInfo 删除ProgramJoinInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (programJoinInfoService *ProgramJoinInfoService)DeleteProgramJoinInfo(programJoinInfo autocode.ProgramJoinInfo) (err error) {
	err = global.GVA_DB.Delete(&programJoinInfo).Error
	return err
}

// DeleteProgramJoinInfoByIds 批量删除ProgramJoinInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (programJoinInfoService *ProgramJoinInfoService)DeleteProgramJoinInfoByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]autocode.ProgramJoinInfo{},"id in ?",ids.Ids).Error
	return err
}

// UpdateProgramJoinInfo 更新ProgramJoinInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (programJoinInfoService *ProgramJoinInfoService)UpdateProgramJoinInfo(programJoinInfo autocode.ProgramJoinInfo) (err error) {
	err = global.GVA_DB.Save(&programJoinInfo).Error
	return err
}

// GetProgramJoinInfo 根据id获取ProgramJoinInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (programJoinInfoService *ProgramJoinInfoService)GetProgramJoinInfo(id uint) (err error, programJoinInfo autocode.ProgramJoinInfo) {
	err = global.GVA_DB.Where("id = ?", id).First(&programJoinInfo).Error
	return
}

// GetProgramJoinInfoInfoList 分页获取ProgramJoinInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (programJoinInfoService *ProgramJoinInfoService)GetProgramJoinInfoInfoList(info autoCodeReq.ProgramJoinInfoSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&autocode.ProgramJoinInfo{}).Order("created_at desc")
    var programJoinInfos []autocode.ProgramJoinInfo
    // 如果有条件搜索 下方会自动创建搜索语句
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }
	err = db.Limit(limit).Offset(offset).Find(&programJoinInfos).Error
	return err, programJoinInfos, total
}
